# index_check.py

import string

from ot_simulator.template.template_funs import frange

def raw(x):
    return "A{0:.3f}".format(x)

def rounded(x):
    return "A{0:.2f}".format(2.0*round(x/2.0,2))

def gen(filename, inc=1.1234, doctor_fn=raw):
    with open(filename + '.nc', 'w') as f:
        print >> f, G_code_start.substitute(name=filename)
        print >> f, "G1 F30"
        for A in frange(inc, 0.0, 360.0):
            print >> f, doctor_fn(A)
        print >> f, "M1"
        for A in frange(-inc, 360.0, 0.0):
            print >> f, doctor_fn(A)
        print >> f, G_code_stop

# only needs ``name``
G_code_start = string.Template(
r"""O$name
G40                                  ( 7 Cancel cutter radius compensation)
G49                                  ( 8 Cancel tool length offset)
G50                                  (   Reset all scale factors to 1.0)
G52 X0 Y0 Z0 A0                      (   Temporary coordinate system offsets)
G69                                  (   Cancel Coordinate system rotation)
G80                                  ( 1 Cancel motion modes)
M9                                   (   All coolant off)
G17                                  ( 2 XY Plane Select, for arcs and canned)
G20                                  ( 6 Inch unit)
G64                                  (13 Constant Velocity mode)
G90                                  ( 3 Absolute distance mode)
M48                                  (   Enable speed and feed override)
G94                                  ( 5 Feed per minute mode)
G54                                  (   Select work offset #1)
""")

G_code_stop = \
r"""M30                                 (   End program and "rewind")
"""

